<template>
  <div class="main-layout">
    <!-- 顶部导航栏 -->
    <el-header class="main-header">
      <div class="header-left">
        <div class="logo">
          <div class="logo-icon">
            <el-icon><Setting /></el-icon>
          </div>
          <h1 class="system-title">海关AI智能管理系统</h1>
        </div>
      </div>
      <div class="header-right">
        <div class="user-info">
          <el-dropdown @command="handleUserCommand">
            <span class="user-dropdown">
              <el-avatar :size="32">
                <el-icon><User /></el-icon>
              </el-avatar>
              <span class="username">{{ userInfo.name }}</span>
              <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>个人信息
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>系统设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div class="system-time">{{ currentTime }}</div>
      </div>
    </el-header>

    <el-container class="main-container">
      <!-- 左侧导航菜单 -->
      <el-aside :width="sidebarWidth" class="main-sidebar">
        <div class="sidebar-header">
          <el-button 
            text 
            @click="toggleSidebar" 
            class="sidebar-toggle"
            :icon="isCollapsed ? Expand : Fold"
          />
        </div>
        
        <el-menu
          :default-active="activeMenu"
          :collapse="isCollapsed"
          :unique-opened="true"
          router
          class="sidebar-menu"
          background-color="#001529"
          text-color="#rgba(255,255,255,.65)"
          active-text-color="#1890ff"
        >
          <!-- 系统概览 -->
          <el-menu-item index="/dashboard">
            <el-icon><Monitor /></el-icon>
            <template #title>系统概览</template>
          </el-menu-item>

          <!-- 智能参数引擎 -->
          <el-sub-menu index="param-engine">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>智能参数引擎</span>
            </template>
            <el-menu-item index="/param-engine/dashboard">
              <el-icon><Monitor /></el-icon>
              <template #title>引擎概览</template>
            </el-menu-item>
            <el-menu-item index="/param-engine/categories">
              <el-icon><Folder /></el-icon>
              <template #title>参数分类</template>
            </el-menu-item>
            <el-menu-item index="/param-engine/definitions">
              <el-icon><Document /></el-icon>
              <template #title>参数定义</template>
            </el-menu-item>
            <el-menu-item index="/param-engine/values">
              <el-icon><Edit /></el-icon>
              <template #title>参数配置</template>
            </el-menu-item>
            <el-menu-item index="/param-engine/rules">
              <el-icon><Star /></el-icon>
              <template #title>规则引擎</template>
            </el-menu-item>
            <el-menu-item index="/param-engine/ai-recommendations">
              <el-icon><Cpu /></el-icon>
              <template #title>AI推荐</template>
            </el-menu-item>
            <el-menu-item index="/param-engine/monitoring">
              <el-icon><DataAnalysis /></el-icon>
              <template #title>系统监控</template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 异常监测系统 -->
          <el-sub-menu index="anomaly-detection" disabled>
            <template #title>
              <el-icon><Warning /></el-icon>
              <span>异常监测系统</span>
              <el-tag size="small" type="info" style="margin-left: 8px;">开发中</el-tag>
            </template>
            <el-menu-item index="/anomaly/detection">
              <el-icon><Search /></el-icon>
              <template #title>异常检测</template>
            </el-menu-item>
            <el-menu-item index="/anomaly/alerts">
              <el-icon><Bell /></el-icon>
              <template #title>预警管理</template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 数据采集模块 -->
          <el-sub-menu index="data-collection" disabled>
            <template #title>
              <el-icon><Upload /></el-icon>
              <span>数据采集模块</span>
              <el-tag size="small" type="info" style="margin-left: 8px;">开发中</el-tag>
            </template>
            <el-menu-item index="/data-collection/ftp">
              <el-icon><Connection /></el-icon>
              <template #title>FTP采集</template>
            </el-menu-item>
            <el-menu-item index="/data-collection/upload">
              <el-icon><UploadFilled /></el-icon>
              <template #title>文件上传</template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 预测分析模块 -->
          <el-sub-menu index="prediction" disabled>
            <template #title>
              <el-icon><TrendCharts /></el-icon>
              <span>预测分析模块</span>
              <el-tag size="small" type="info" style="margin-left: 8px;">开发中</el-tag>
            </template>
            <el-menu-item index="/prediction/arima">
              <el-icon><TrendCharts /></el-icon>
              <template #title>ARIMA预测</template>
            </el-menu-item>
            <el-menu-item index="/prediction/trends">
              <el-icon><TrendCharts /></el-icon>
              <template #title>趋势分析</template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 查询分析模块 -->
          <el-sub-menu index="query-analysis" disabled>
            <template #title>
              <el-icon><Search /></el-icon>
              <span>查询分析模块</span>
              <el-tag size="small" type="info" style="margin-left: 8px;">开发中</el-tag>
            </template>
            <el-menu-item index="/query/data">
              <el-icon><Grid /></el-icon>
              <template #title>数据查询</template>
            </el-menu-item>
            <el-menu-item index="/query/reports">
              <el-icon><Tickets /></el-icon>
              <template #title>报表统计</template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 系统管理 -->
          <el-sub-menu index="system" disabled>
            <template #title>
              <el-icon><Tools /></el-icon>
              <span>系统管理</span>
              <el-tag size="small" type="info" style="margin-left: 8px;">开发中</el-tag>
            </template>
            <el-menu-item index="/system/users">
              <el-icon><UserFilled /></el-icon>
              <template #title>用户管理</template>
            </el-menu-item>
            <el-menu-item index="/system/roles">
              <el-icon><Key /></el-icon>
              <template #title>角色权限</template>
            </el-menu-item>
            <el-menu-item index="/system/logs">
              <el-icon><Document /></el-icon>
              <template #title>系统日志</template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>

      <!-- 主内容区域 -->
      <el-main class="main-content">
        <!-- 面包屑导航 -->
        <div class="breadcrumb-container">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item 
              v-for="item in breadcrumbItems" 
              :key="item.path"
              :to="item.path"
            >
              {{ item.title }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <!-- 页面内容 -->
        <div class="content-wrapper">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  User, ArrowDown, Setting, SwitchButton, Monitor, Folder, Document, 
  Edit, Star, Cpu, DataAnalysis, Warning, Search, Bell, Upload, 
  Connection, UploadFilled, TrendCharts, Grid, Tickets, 
  Tools, UserFilled, Key, Expand, Fold
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

// 侧边栏状态
const isCollapsed = ref(false)
const sidebarWidth = computed(() => isCollapsed.value ? '64px' : '240px')

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 用户信息
const userInfo = ref({
  name: '系统管理员'
})

// 当前时间
const currentTime = ref('')
let timeInterval = null

// 面包屑导航
const breadcrumbItems = computed(() => {
  const pathArray = route.path.split('/').filter(item => item)
  const items = []
  
  // 根据路径生成面包屑
  const routeMap = {
    'param-engine': '智能参数引擎',
    'dashboard': '概览',
    'categories': '参数分类',
    'definitions': '参数定义',
    'values': '参数配置',
    'rules': '规则引擎',
    'ai-recommendations': 'AI推荐',
    'monitoring': '系统监控'
  }
  
  let currentPath = ''
  pathArray.forEach(segment => {
    currentPath += `/${segment}`
    if (routeMap[segment]) {
      items.push({
        path: currentPath,
        title: routeMap[segment]
      })
    }
  })
  
  return items
})

// 切换侧边栏
const toggleSidebar = () => {
  isCollapsed.value = !isCollapsed.value
}

// 用户下拉菜单命令处理
const handleUserCommand = (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人信息功能开发中')
      break
    case 'settings':
      ElMessage.info('系统设置功能开发中')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '退出确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 清除用户信息和token
    localStorage.removeItem('token')
    ElMessage.success('已成功退出登录')
    router.push('/login')
  }).catch(() => {
    // 用户取消操作
  })
}

// 更新时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

onMounted(() => {
  updateTime()
  timeInterval = setInterval(updateTime, 1000)
})

onUnmounted(() => {
  if (timeInterval) {
    clearInterval(timeInterval)
  }
})
</script>

<style scoped>
.main-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 顶部导航栏 */
.main-header {
  background: #001529;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 64px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  background: #1890ff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-icon .el-icon {
  font-size: 24px;
  color: white;
}

.system-title {
  font-size: 20px;
  font-weight: bold;
  margin: 0;
  color: white;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 24px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.username {
  color: white;
  font-size: 14px;
}

.dropdown-icon {
  color: rgba(255, 255, 255, 0.65);
  font-size: 12px;
}

.system-time {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.65);
  font-family: 'Courier New', monospace;
}

/* 主容器 */
.main-container {
  flex: 1;
  height: calc(100vh - 64px);
}

/* 侧边栏 */
.main-sidebar {
  background: #001529;
  transition: width 0.3s;
}

.sidebar-header {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 16px;
  border-bottom: 1px solid #1f1f1f;
}

.sidebar-toggle {
  color: rgba(255, 255, 255, 0.65);
}

.sidebar-toggle:hover {
  color: #1890ff;
}

.sidebar-menu {
  border: none;
  height: calc(100% - 48px);
  overflow-y: auto;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 240px;
}

/* 主内容区域 */
.main-content {
  background: #f0f2f5;
  padding: 0;
  overflow: hidden;
}

.breadcrumb-container {
  background: white;
  padding: 16px 24px;
  border-bottom: 1px solid #e8e8e8;
  margin-bottom: 0;
}

.content-wrapper {
  padding: 24px;
  height: calc(100% - 57px);
  overflow-y: auto;
}

/* 菜单样式覆盖 */
:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 48px;
  line-height: 48px;
}

:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  background-color: rgba(24, 144, 255, 0.1) !important;
}

:deep(.el-menu-item.is-active) {
  background-color: rgba(24, 144, 255, 0.15) !important;
  border-right: 3px solid #1890ff;
}

:deep(.el-sub-menu .el-menu-item) {
  height: 40px;
  line-height: 40px;
  padding-left: 58px !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-header {
    padding: 0 16px;
  }
  
  .system-title {
    font-size: 16px;
  }
  
  .system-time {
    display: none;
  }
  
  .content-wrapper {
    padding: 16px;
  }
}
</style>


